package com.cksys.achievement.controller;


import com.cksys.achievement.entity.Setting;
import com.cksys.achievement.service.ISettingService;
import com.cksys.achievement.utils.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tadechen
 * @since 2019-09-26
 */
@RestController
@RequestMapping("/setting")
public class SettingController {

    @Autowired
    ISettingService settingService;

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/register")
    public RestResponse updateRegister(@RequestParam(required = false, defaultValue = "0") Boolean flag) {
        boolean b = settingService.updateById(new Setting().setId(1).setIsRegister(flag));
        if (b) {
            return RestResponse.ok(flag, "修改成功");
        }
        return RestResponse.fail("修改失败");
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @GetMapping("/getAll")
    public RestResponse getAll() {
        Setting byId = settingService.getById(1);
        return RestResponse.ok(byId);
    }

    @PreAuthorize("hasAnyRole('ROLE_ADMIN', 'ROLE_SUPER')")
    @PostMapping("/update")
    public RestResponse update(Setting setting) {
        setting.setId(1);
        boolean b = settingService.updateById(setting);
        if (b) {
            return RestResponse.ok(setting);
        } else {
            return RestResponse.fail();
        }
    }
}
